﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Premiere.DTO;
using System.Collections;
using Premiere.Window;
using System.Data;

namespace PremiereMX
{
    public class ExchangeRateLogic
    {
        ExchangeRateDTO m_subject = new ExchangeRateDTO();
        IMainForm m_mainForm;
        IUserControlExchangeRate m_form;
        EditMode m_editMode = EditMode.OK;
        public ExchangeRateLogic(IMainForm mainForm, IUserControlExchangeRate form)
        {
            m_mainForm = mainForm;
            m_form = form;
        }
        public void Init()
        {
            //updateForm();
            //m_form.SetToReadOnly(true);
            //SetButtonEditMode(EditMode.OK);
            ChangeDate(DateTime.Today);
            //updateExchangeRateList();
        }
        //private void updateForm()
        //{
        //    m_form.LastUpdate = m_subject.LastUpdate;
        //    m_form.ComputerName = m_subject.ComputerName;
        //    m_form.UserUpdateBy = m_subject.UserName;
        //}
        private void SetButtonEditMode(EditMode editMode)
        {
            m_form.SetButtonEditMode(editMode);
            m_editMode = editMode;
        }
        //private void updateExchangeRateList()
        //{
        //    IList list = ServiceRegistry.GetInstance().GetExchangeRateService().LoadAll();
        //    m_form.UpdateList(list);
        //}
        public void Edit()
        {
            m_form.SetToReadOnly(false);
            m_form.SetButtonEditMode(EditMode.Insert);
        }
        private void updateSubject()
        {

            m_subject.ComputerName = Environment.MachineName;
            m_subject.UserName = m_mainForm.GetUser().Code;       

            m_subject.LastUpdate = DateTime.Now;
        }
        //internal void UpdateSubject(ExchangeRateDTO curr)
        //{
        //    m_subject = curr;
        //    m_form.ClearForm();
        //    updateForm();
        //    m_form.SetToReadOnly(true);
        //    SetButtonEditMode(EditMode.OK);
        //}
        public void Save()
        {
            try
            {
                IList items = m_form.GetUpdatedItems();
                foreach (ExchangeRateDTO dto in items)
                {
                    dto.UserName = m_mainForm.GetUser().Code;       
                    dto.ComputerName = Environment.MachineName;
                    dto.LastUpdate = DateTime.Now;
                    dto.Id = ServiceRegistry.GetInstance().GetExchangeRateService().Save(dto);
                }
                m_form.ShowMessage("Record has been saved!");
                SetButtonEditMode(EditMode.OK);
                m_form.SetToReadOnly(true);
            }
            catch (Exception ex)
            {
                m_form.ShowMessage(ex.Message);
            }
        }
        internal void Delete()
        {
            //if (m_subject.Id == 0) return;
            //ServiceRegistry.GetInstance().GetExchangeRateService().Delete(m_subject);
            //m_form.RemoveItem(m_subject.Id);
            //m_form.ClearForm();
        }
        internal void New()
        {
            //m_subject = new ExchangeRateDTO();
            //m_form.ClearForm();
            //SetButtonEditMode(EditMode.Insert);
            //m_form.SetToReadOnly(false);
        }
        public void Refresh()
        {
        }
        internal void Print()
        {
            DataSet ds = ServiceRegistry.GetInstance().GetCurrencyService().GetPreviewReportData(m_subject.Id);
            m_mainForm.Print(ds);
        }
        private IList getAllCurrency()
        {
            IList result = ServiceRegistry.GetInstance().GetCurrencyService().LoadAll();
            return result;
        }
        public void ChangeDate(DateTime date)
        {
            m_form.ClearItems();
            IList result = ServiceRegistry.GetInstance().GetExchangeRateService().FindExchangeRateByDate(date.Date);
            IList ccys = getAllCurrency();
            if (result.Count == 0)
            {
                foreach (CurrencyDTO ccy in ccys)
                {
                    ExchangeRateDTO dto = new ExchangeRateDTO();
                    dto.Date = date.Date;
                    dto.BuyRate = 0m;
                    dto.Currency = ccy;
                    dto.LastUpdate = DateTime.Now;
                    dto.UserName = m_mainForm.GetUser().Code;     
                    dto.ComputerName = Environment.MachineName;
                    dto.MiddleRate = 0m;
                    dto.SellRate = 0m;
                    m_form.AddExchangeRate(dto);
                }
                m_form.ComputerName = Environment.MachineName;
                m_form.UserUpdateBy = m_mainForm.GetUser().Code;       
                m_form.LastUpdate = DateTime.Now;
            }
            else
            {
                foreach (ExchangeRateDTO dto in result)
                {
                    m_form.AddExchangeRate(dto);
                }
                IList ccysAdd = new ArrayList();
                foreach (ExchangeRateDTO dto in result)
                {
                    foreach (CurrencyDTO ccy in ccys)
                    {
                        if (dto.Currency.Id == ccy.Id)
                        {
                            ccysAdd.Add(ccy);
                            break;
                        }
                    }
                }
                foreach (CurrencyDTO ccy in ccysAdd)
                {
                    ccys.Remove(ccy);
                }
                foreach (CurrencyDTO ccy in ccys)
                {
                    ExchangeRateDTO dto = new ExchangeRateDTO();
                    dto.Date = date;
                    dto.BuyRate = 0m;
                    dto.Currency = ccy;
                    dto.LastUpdate = DateTime.Today.Date;
                    dto.MiddleRate = 0m;
                    dto.SellRate = 0m;
                    dto.UserName = m_mainForm.GetUser().Code;     
                    dto.ComputerName = Environment.MachineName;
                    m_form.AddExchangeRate(dto);
                }
                m_form.ComputerName = ((ExchangeRateDTO)result[0]).ComputerName;
                m_form.UserUpdateBy = ((ExchangeRateDTO)result[0]).UserName;
                m_form.LastUpdate = ((ExchangeRateDTO)result[0]).LastUpdate;
            }
            m_form.SetToReadOnly(true);
            SetButtonEditMode(EditMode.OK);
            //m_currencyDate = date;
        }
        public void CopyFromDate(DateTime date)
        {
            IList result = ServiceRegistry.GetInstance().GetExchangeRateService().FindExchangeRateByDate(date.Date);
            if (result.Count == 0) return;
            foreach (ExchangeRateDTO dto in result)
            {
                m_form.AddExchangeRateCopy(dto);
            }
        }
    }
}
